package com.gorkr.hot100.simple;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gorkr
 * @date 2022/03/15 09:03
 * 多数元素
 **/
public class L169 {
    public int majorityElement(int[] nums) {

        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num:
             nums) {
            if(map.containsKey(num)){
                map.put(num,map.get(num)+1);
            }else {
                map.put(num,1);
            }
        }
        int flag = nums.length/2;
        for(Map.Entry<Integer,Integer> entry: map.entrySet()){
            if(entry.getValue()>flag){
                return entry.getKey();
            }
        }

        return 0;
    }
}
    